using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace Main
{
    public class Poll
    {
#region "Constructors"
        public Poll()
        {        }
        public Poll(string p_sTitle)
        {
            Title = p_sTitle;
        }
        public Poll(string p_sTitle, string p_sQuestion)
        {
            Title = p_sTitle;
            Question = p_sQuestion;
        }
        public Poll(string p_sTitle, string p_sQuestion, string p_sSummary)
        {
            Title = p_sTitle;
            Question = p_sQuestion;
            Summary = p_sSummary;
        }
#endregion
        
#region "Properties"
        private int _ID = 0;
        public int ID
        {
            get
            {
                return _ID;
            }
            set
            {
                _ID = value;
            }
        }

        private string _Title = "";
        public string Title
        {
            get
            {
                return _Title;
            }
            set
            {
                _Title = value;
            }
        }

        private string _Question = "";
        public string Question
        {
            get
            {
                return _Question;
            }
            set
            {
                _Question = value;
            }
        }

        private string _Summary = "";
        public string Summary
        {
            get
            {
                return _Summary;
            }
            set
            {
                _Summary = value;
            }
        }

        private DateTime _CreatedOn;
        public DateTime CreatedOn
        {
            get
            {
                return _CreatedOn;
            }
            set
            {
                _CreatedOn = value;
            }
        }

        private DateTime _StartDate;
        public DateTime StartDate
        {
            get
            {
                return _StartDate;
            }
            set
            {
                _StartDate = value;
            }
        }

        private DateTime _EndDate;
        public DateTime EndDate
        {
            get
            {
                return _EndDate;
            }
            set
            {
                _EndDate = value;
            }
        }

        private OptionCollection _Options = new OptionCollection();
        public OptionCollection Options
        {
            get
            { return _Options; }
            set
            { _Options = value; }
        }

        private Guid _UserID;
        public Guid UserID
        {
            get
            { return _UserID; }
            set
            { _UserID = value; }
        }

        private bool _IsPublic;
        public bool IsPublic
        {
            get
            { return _IsPublic; }
            set
            { _IsPublic = value; }
        }
#endregion
        
#region "Data Access Methods"

        public void FillByID(int p_ID)
        {
            //Pulls the data from the database
            try
            {
                DataTable dtPoll = DataAccess.GetPollByID(p_ID);

                //Only the first row is important
                ID = (int)dtPoll.Rows[0][dtPoll.Columns.IndexOf("PollId")];
                Title = (string)dtPoll.Rows[0][dtPoll.Columns.IndexOf("Title")];
                Question = (string)dtPoll.Rows[0][dtPoll.Columns.IndexOf("Question")];
                Summary = (string)dtPoll.Rows[0][dtPoll.Columns.IndexOf("Summary")];
                CreatedOn = (DateTime)dtPoll.Rows[0][dtPoll.Columns.IndexOf("CreatedOn")];
                StartDate = (DateTime)dtPoll.Rows[0][dtPoll.Columns.IndexOf("StartDate")];
                EndDate = (DateTime)dtPoll.Rows[0][dtPoll.Columns.IndexOf("EndDate")];
                UserID = (Guid)dtPoll.Rows[0][dtPoll.Columns.IndexOf("UserID")];
                IsPublic = (bool)dtPoll.Rows[0][dtPoll.Columns.IndexOf("IsPublic")];

                Options.FillByPollId(ID);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        public void Commit()
        {
            try
            {
                DataTable dt = DataAccess.CommitPoll(Title, Question, Summary, StartDate, EndDate, UserID, IsPublic);
                ID = (int)dt.Rows[0]["PollID"];

                foreach (Option option in Options)
                {
                    DataAccess.CommitPollOption(ID, option.PollOption);
                }
            }
            catch (Exception e)
            {
                String test = e.Message;
            }
        }
    
#endregion

#region "Custom Methods"
        public void Log(TextWriter stream)
        {
            stream.WriteLine("PollID: " + ID);
            stream.WriteLine("Title: " + Title);
            stream.WriteLine("Question: " + Question);
            stream.WriteLine("Summary: " + Summary);

            foreach (Option option in Options)
            {
                option.Log(stream);
            }
        }

        //Writes the xml out as a string
        public string WriteXml()
        {
            StringWriter sw = new StringWriter();
            XmlSerializer xs = new XmlSerializer(typeof(Poll));

            xs.Serialize(sw, this);
            return sw.ToString();
        }

        //Write the xml out to a stream (file or other)
        public bool WriteXml(Stream inStream)
        {
            XmlSerializer xs = new XmlSerializer(typeof(Poll));

            //Try to serialize to the stream
            try
            {
                xs.Serialize(inStream, this);
                return true;
            }
            //if we fail return false
            catch
            {
                return false;
            }
        }

        //Fill the Poll from an xml string
        public void FillFromXml(string xml)
        {
            //Fill the xml reader from a string reader filled with the passed in string
            XmlTextReader xr = new XmlTextReader(new StringReader(xml));
            XmlSerializer xs = new XmlSerializer(typeof(Poll));

            Poll tempPoll = (Poll)xs.Deserialize(xr);

            this.ID = tempPoll.ID;
            this.Question = tempPoll.Question;
            this.Summary = tempPoll.Summary;
            this.StartDate = tempPoll.StartDate;
            this.EndDate = tempPoll.EndDate;
            this.Title = tempPoll.Title;
            this.Options = tempPoll.Options;
            this.CreatedOn = tempPoll.CreatedOn;
        }
#endregion

    }

    public class PollCollection : CollectionBase
    {
        public void Add(Poll p_Poll)
        {
            InnerList.Add(p_Poll);
        }
        public void Remove(int p_iIndex)
        {
            InnerList.Remove(p_iIndex);
        }

        public Poll this[int index]
        {
            get
            { return (Poll)InnerList[index]; }
        }

        public void FillAllWithOptions()
        {
            DataTable dtResults = DataAccess.GetAllPollsWithOptions();

            int currentPoll = 0;
            foreach (DataRow row in dtResults.Rows)
            {
                if (currentPoll != (int)row["PollID"])
                {
                    currentPoll = (int)row["PollID"];
                    Poll newPoll = new Poll();
                    newPoll.FillByID(currentPoll);
                }
            }
        }

        public void FillAllForUser(Guid p_UserID)
        {
            DataTable dtResults = DataAccess.GetPollsForUser(p_UserID);

            foreach (DataRow row in dtResults.Rows)
            {
                Poll newPoll = new Poll();
                newPoll.FillByID((int)row["PollID"]);
                Add(newPoll);
            }
        }

        public void FillAllForFriends(Guid p_UserID, int limit)
        {
            DataTable dtResults = DataAccess.GetPollsForFriendsWithLimit(p_UserID,limit);

            foreach (DataRow row in dtResults.Rows)
            {
                Poll newPoll = new Poll();
                newPoll.FillByID((int)row["PollID"]);
                Add(newPoll);
            }
        }
    }
}